<?php
/**
 * @fileoverview    封装评论模块
 * @author          gaojian3
 * @date            2012-04-12
 * @copyright       sina meila
 * @param           
 * @param             
 * @return     
 * 还没有封装完，手机版够用
 */

include(SERVER_ROOT_PATH.'/include/config/dataRule/ml_datarule_content.php');
class ml_biz_comment {
    
    public $result;
    public function __construct($data){
        $this->_rid=$data['rid'];
        $this->_content = $data['content'];
        $this->_comid=$data['comid'];
        $this->_act_uid=$data['act_uid'];
        $this->_from=$data['from'];
        $this->oComment=new ml_model_dbComment();
    }
    public function checkParam(){
        $this->_uid=ml_tool_resid::resid2uid($this->_rid);
        if(empty($this->_content_rid) && ml_tool_resid::resid2type($this->_rid)==ML_RESID_TYPE_CONTENT){
            $this->_content_rid = $this->_rid;
        }
        $rs=$this->checkContent();
        if(!$rs){
            return $this->result;
        }
        if(!empty($this->_comid)){
            $rs=$this->checkReplyNick();
            if(!$rs){
                return $this->result;
            }
        }
        $this->result['code']=ML_RCODE_SUCC;
        return $this->result;
    }
    public function main(){
        $this->data=array(
            'rid'=>$this->_rid,
            'uid'=>$this->_uid,
            'act_uid'=>$this->_act_uid,
            'content'=>$this->_content,
            'content_rid'=>$this->_content_rid,
            'from'=>$this->_from,
            'reply_id'=>$this->_comid,
        );
        $rs=$this->oComment->insertComment($this->data);
        if(!$rs){
            $this->result['code']=ML_RCODE_BUSY;
            $this->result['msg']='insert comment';
            return $this->result;
        }
        $rs=$this->incrComCount($this->_rid);
        if(!$rs){
            return $this->result;
        }
        $this->_cmt_id=$this->oComment->insert_id();   //最后插入的评论id
        $totalNum= $this->getComCountNum($this->_rid);
        
        $back=array(
            'comid'=> $this->_cmt_id,
            'totalNum'=> $totalNum ? $totalNum : 0 
            );
        $this->addNotice();
        $this->result['code']=ML_RCODE_SUCC;
        $this->result['data']=$back;
        return $this->result;
    }
    
    public function  addNotice(){
        $oRds = new ml_model_rdsNotice();
        $aAtUids=$this->getUids();
        $oRds->add_notice($this->_uid , ml_model_rdsNotice::NTCTYPE_ADDCOMMENT);
        if(ml_tool_resid::resid2type($this->_rid)==ML_RESID_TYPE_CONTENT){
            if(!empty($this->_comid)){
                ml_tool_sendMq::atme_in_replycomment($this->_act_uid,$this->_rid,$this->_cmt_id,$this->oldComment['act_uid'],$this->data['reply_id'],$aAtUids);
            }else{
                ml_tool_sendMq::atme_in_newcomment($this->_act_uid,$this->_rid,$this->_cmt_id,$aAtUids);
            }
        }
        ml_tool_sendMq::userAction2GoodsRank($this->_rid , 'cmt');
        //行为日志
        switch($this->from){
            case ML_FROM_WEB:
                $from='0';
                break;
            case ML_FROM_PHONE:
                $from= MLAPP_FROM_VDAPEI;
                break;
        }
        ml_tool_actionlog::add_cmt($this->_act_uid,$this->_rid,$this->_content,$from,count($aAtUids));
        
    }
    
    public function insertComment($data){
        $rs=$this->oComment->insertComment($data);
        if(!$rs){
            $this->result['code']=ML_RCODE_BUSY;
            $this->result['msg']='insert comment fail';    
            return false;
        }else{
            return true;
        }
    }
    
    //@todo 后期再封装
    public function insertFeed(){
        
        if(!empty($this->_content_rid)){
            $this->data['rid'] = $this->_content_rid;
            $feed_uid=$this->data['uid'];
            $content_uid = ml_tool_resid::resid2uid($data['rid']);
            $this->data['uid']=$content_uid;
            if(!empty($this->_comid)){
                $this->data['reply_id']=$this->data['reply_id']+1; //    暂时这样获取回复评论的id 
            }
            $rs=$this->insertComment($this->data);
            if(!$rs){
                return $this->result;
            }
            $this->incrComCount($this->data['rid']);
            $this->_conmt_id=$this->oComment->insert_id(); 
            
            if($content_uid == $feed_uid){
                $this->oComment->updateUid($content_uid,$this->_conmt_id);  //@todo 以后队列
            }else{
                $aAtUids=$thisgetUids();
                if(!empty($this->_comid)){
                    ml_tool_sendMq::atme_in_replycomment($this->_act_uid,$this->data['rid'],$this->_conmt_id,$this->oldComment['act_uid'],$this->data['reply_id'],$aAtUids);
                }else{
                    ml_tool_sendMq::atme_in_newcomment($this->_act_uid,$this->data['rid'],$this->_conmt_id,$aAtUids);    
                }
                $oRds = new ml_model_rdsNotice();
                $oRds->add_notice($content_uid , ml_model_rdsNotice::NTCTYPE_ADDCOMMENT);
            }
        }
    }
    
    public function checkContent(){
        if(empty($this->_content)){
            $this->result['code']=ML_RCODE_PARAM;
            $this->result['msg']='content';
            return false;
        }
        if(ml_tool_keyword::haveSensitiveWord($this->_content)){
            $this->result['code']=ML_RCODE_PARAM;
            $this->result['msg']='sensitive';
            return false;
        }
        if(Tool_string::str_width($this->_content) > ML_DATARULE_COMMENT_MAXLEN){
            $this->result['code']=ML_RCODE_PARAM;
            $this->result['msg']='content_len';
            return false;
        }else{
            return true;
        }
    }
    public function checkReplyNick(){
        $this->oComment->getCommentById($this->_uid,$this->_comid);
        $this->oldComment=$this->oComment->get_data();
    
        $nickArr=ml_tool_nick::uids2nicks(array(0=>$this->oldComment['act_uid']));
        $reg='/@([^\s:]+)\s?:/';
        @preg_match($reg,$this->_content,$match);
        if($match[1]!= $nickArr[$this->oldComment['act_uid']]){
            $this->result['code']=ML_RCODE_PARAM;
            $this->result['msg']='reply nick';
            return false;
        }else{
            return true;
        }
    }
    public function incrComCount($rid){
        if(!isset($oRedis)){
            $oRedis=new ml_model_rdsCount();
        }
        $rs = $oRedis->incrCommentCount($rid);
        if(!$rs){
            $this->result['code']=ML_RCODE_BUSY;
            $this->result['msg']='incr com count';
            return false;
        }else{
            return true;
        }
    }
    public function getComCountNum($rid){
        if(!isset($oRedis)){
            $oRedis=new ml_model_rdsCount();
        }
        $aRid=array(
            0 =>$rid    
        );
        $rs = $oRedis->getCommentCount($aRid);
        if(!$rs){
            $this->result['code']=ML_RCODE_BUSY;
            $this->result['msg']='get com count fail';
            return false;
        }
        $count=$oRedis->get_data();
        return $count[$rid];
    }
    
    public function getUids(){
        $aNicks = ml_tool_nick::findAllAtNick($this->_content);
        $aAtUids=array();
        if(count($aNicks) > 0 )
            $aAtUids = ml_tool_nick::nicks2uids($aNicks);
        return     $aAtUids;
    }
}